package cn.edu.gduf.luowowo.member.service.impl;

import cn.edu.gduf.luowowo.member.domain.Item;
import cn.edu.gduf.luowowo.member.domain.ItemOrder;
import cn.edu.gduf.luowowo.member.domain.UserInfo;
import cn.edu.gduf.luowowo.member.mapper.ItemMapper;
import cn.edu.gduf.luowowo.member.mapper.ItemOrderMapper;
import cn.edu.gduf.luowowo.member.mapper.UserInfoMapper;
import cn.edu.gduf.luowowo.member.service.IItermOrderService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;
import java.util.List;

@Service
public class ItermOrderServiceImpl implements IItermOrderService {
    @Autowired
    private ItemOrderMapper itemOrderMapper;
    @Autowired
    private ItemMapper  itemMapper;
    @Autowired
    private UserInfoMapper userInfoMapper;

    @Override
    public void insertOrder(Long itermId, Long userInfoId) {
        Item item = itemMapper.selectByPrimaryKey(itermId);
        userInfoMapper.updateCoinsNum(-item.getPrice(),userInfoId);
        UserInfo userInfo = userInfoMapper.selectByPrimaryKey(userInfoId);
        ItemOrder itemOrder = new ItemOrder();
        itemOrder.setBuyer(userInfo);
        itemOrder.setItem(item);
        itemOrder.setCreateTime(new Date());
        itemOrder.setGoodsMoney(item.getPrice());
        itemOrderMapper.insert(itemOrder);
    }

    @Override
    public List<ItemOrder> selectByUserId(Long id) {
        return itemOrderMapper.selectByUserId(id);
    }
}
